package com.wanxi.service.impl;

import com.alibaba.fastjson.JSON;
import com.wanxi.dto.ProductDetailDTO;
import com.wanxi.feign.DoctorFeignClient;
import com.wanxi.feign.ProductFeignClient;
import com.wanxi.vo.DoctorQuery;
import com.wanxi.model.ProductQuery;
import com.wanxi.result.CommonResult;
import com.wanxi.service.FrontProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
@Service
public class FrontProductServiceImpl  implements FrontProductService {

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private DoctorFeignClient doctorFeignClient;

    @Override
    public CommonResult findProduct(ProductQuery productQuery) {
        return productFeignClient.findProduct(productQuery);
    }

    @Override
    public CommonResult findProductDetail(Long productId) {
        //查询服务包信息
        CommonResult commonResult = productFeignClient.findProductDetail(productId);
        if (commonResult.getCode() == 200){
            List<LinkedHashMap> productDetailVoHashMap = (List<LinkedHashMap>) commonResult.getData();
            LinkedHashMap hashMap = productDetailVoHashMap.get(0);
            ProductDetailDTO productDetailDTO = JSON.parseObject(JSON.toJSONString(hashMap), ProductDetailDTO.class);
            DoctorQuery doctorQuery = new DoctorQuery();
            doctorQuery.setProductId(productDetailDTO.getId());
            //查询服务包下医生团队信息
            CommonResult doctorCommonResult = doctorFeignClient.findAllFront(doctorQuery);
            if (doctorCommonResult.getCode() == 200){
                List<Object> doctors = (ArrayList<Object>) doctorCommonResult.getData();
                productDetailDTO.setDoctorList(doctors);
                return CommonResult.success(productDetailDTO);
            }else {
                return CommonResult.failed();
            }
        }else {
            return CommonResult.failed();
        }
    }
}
